﻿using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(PUEXParticleSystem))]
public class PUEXParticleSystemEditor : Editor {

	[MenuItem("PUEX/NewParticleSystem")]
	public static void NewParticleSystem ()
	{
		new GameObject ("New ParticleSystem", typeof(PUEXParticleSystem));
	}

	protected string _eventName = "test_event";
	protected string _emitterName = "";
	protected int _emitCount = 10;
	
	public override void OnInspectorGUI ()
	{
		var system = target as PUEXParticleSystem;

		DrawDefaultInspector ();

		EditorGUILayout.Space ();
		if (GUILayout.Button ("Add new emitter")) {
			var newEmitter = new GameObject ("Emitter " + (system.transform.childCount + 1));
			newEmitter.transform.SetParent (system.transform);
			
			Undo.RecordObject (newEmitter, "Add new emitter");
			EditorUtility.SetDirty (newEmitter);
		}
		if (GUILayout.Button ("Add sub system")) {
			var newSystem = new GameObject ("SubSystem " + (system.transform.childCount + 1), typeof(PUEXParticleSystem));
			newSystem.transform.SetParent (system.transform);
			
			Undo.RecordObject (newSystem, "Add sub system");
			EditorUtility.SetDirty (newSystem);
		}
	}

	public void OnSceneGUI ()
	{
		if (Application.isPlaying) {
			var system = target as PUEXParticleSystem;

			Handles.BeginGUI ();
			GUILayout.BeginArea (new Rect (Screen.width - 200, Screen.height - 200, 190, 155), EditorStyles.textArea);
			GUILayout.BeginVertical ();

			GUILayout.Label ("PUEX Particle Effect");

			GUILayout.BeginHorizontal ();
			if (system.isRuned ()) {
				if (system.isPaused ()) {
					if (GUILayout.Button ("Resume", EditorStyles.miniButtonLeft, GUILayout.Width (90f), GUILayout.Height (20f))) {
						resumeParticleSystemInEditor ();
					}
				} else {
					if (GUILayout.Button ("Pause", EditorStyles.miniButtonLeft, GUILayout.Width (90f), GUILayout.Height (20f))) {
						pauseParticleSystemInEditor ();
					}
				}
			} else {
				if (GUILayout.Button ("Simulate", EditorStyles.miniButtonLeft, GUILayout.Width (90f), GUILayout.Height (20f))) {
					startParticleSystemInEditor ();
				}
			}

			if (GUILayout.Button ("Stop", EditorStyles.miniButtonRight, GUILayout.Width (90f), GUILayout.Height (20f))) {
				stopParticleSystemInEditor ();
			}
			GUILayout.EndHorizontal ();

			GUILayout.Space (5f);
		
			GUILayout.BeginHorizontal ();
			GUILayout.Label ("Playback Speed:", GUILayout.Width (90f));
			EditorGUI.BeginChangeCheck ();
			string playSpeed = GUILayout.TextField (system.playSpeed.ToString (), GUILayout.Width (40));
			if (EditorGUI.EndChangeCheck ()) {
				system.playSpeed = float.Parse (playSpeed);
			}
			if (GUILayout.Button ("+", EditorStyles.miniButtonLeft, GUILayout.Width (20f))) {
				system.playSpeed += 0.1f;
			}
			if (GUILayout.Button ("-", EditorStyles.miniButtonRight, GUILayout.Width (20f))) {
				system.playSpeed -= 0.1f;
			}
			GUILayout.EndHorizontal ();

			GUILayout.BeginHorizontal ();
			GUILayout.Label ("Playback Time:", GUILayout.Width (90f));
			GUILayout.TextField (system.timeElapsedSinceStart.ToString (), GUILayout.Width (85f));
			GUILayout.EndHorizontal ();

			GUILayout.Label ("Particle Count:     " + system.getAliveParticleCount (), GUILayout.Width (180f));
		
			GUILayout.Space (5f);

			GUILayout.BeginHorizontal ();
			_eventName = GUILayout.TextField (_eventName, GUILayout.Width (90f));
			if (GUILayout.Button ("Notify Event", GUILayout.Width (85f))) {
				system.notifyEvent(_eventName);
			}
			GUILayout.EndHorizontal ();

			GUILayout.Space (5f);

			GUILayout.BeginHorizontal ();
			_emitterName = GUILayout.TextField (_emitterName, GUILayout.Width (60f));
			_emitCount = int.Parse(GUILayout.TextField (_emitCount.ToString(), GUILayout.Width (60f)));
			if (GUILayout.Button ("Emit", GUILayout.Width (55f))) {
				if (_emitterName.Length == 0) {
					system.forceEmission(_emitCount);
				} else {
					system.forceEmission(_emitterName, _emitCount);
				}
			}
			GUILayout.EndHorizontal ();

			GUILayout.EndVertical ();
			GUILayout.EndArea ();
			Handles.EndGUI ();
		}
	}

	protected void startParticleSystemInEditor () {
		var system = target as PUEXParticleSystem;
		system.startParticleSystem();
	}

	protected void stopParticleSystemInEditor () {
		var system = target as PUEXParticleSystem;
		system.stopParticleSystem();
	}

	protected void resumeParticleSystemInEditor () {
		var system = target as PUEXParticleSystem;
		system.resumeParticleSystem();
	}

	protected void pauseParticleSystemInEditor () {
		var system = target as PUEXParticleSystem;
		system.pauseParticleSystem();
	}

}
